var db = require('../../config/db')
//查询
exports.search = (req,res)=>{
 
    if(req.query.tar == '1'){
      var sql = `select * from land_resource where userid=?`
      db.query(sql, req.query.id,(err, results)=>{
          if(err) return res.cc('500', err)
          if(results.length < 1) return res.cc('404', '记录为空')
          res.send({ //返回前端信息
              status: 200,
              message: '查询成功',
              data: results,
          })
      })

    }
  else{
    var sql = `select * from land_resource`
    db.query(sql, (err, results)=>{
        //results.owner = '';
        if(err) return res.cc('500', err)
        if(results.length < 1) return res.cc('404', '记录为空')
        for (let i = 0; i < results.length; i++) {
            // 使用闭包固定 i 的值
            (function(index) {
              db.query(`select * from user_info where id = ?`, results[index].userid, (err, result) => {
                if (err) {
                  console.error(err);
                  return;
                }
                results[index].owner = result[0].name;
                // 当所有异步操作完成后，进行后续处理
                if (index === results.length - 1) {
                  // 所有查询操作已完成
                  res.send({ //返回前端信息
                    status: 200,
                    message: '查询成功',
                    data: results,
                })
                }
              });
            })(i);
          } 
    })
  }
    


}
//添加
exports.add = (req,res)=>{//有错误，管理员添加应该提醒重名
    var langResourceMes = req.body 
    if(langResourceMes.tar == '1'){
      //用户添加
      db.query('select * from land_resource where name=?', langResourceMes.name, (err, result)=>{
        if(err) return res.cc('404', err.message)
        if(result.length >=  1) return res.cc('404', '添加失败，土地资源名称存在，请更换')
        var sql = 'insert into landres_review set ?'
        db.query(sql, {landresname:langResourceMes.name, status:'待审核',parcelcount:langResourceMes.parcelcount, address:langResourceMes.address, userid:langResourceMes.userid}, (err, results)=>{
            if(err) return res.cc('404', err.message)
            if(results.affectedRows !== 1) return res.cc('404', '添加失败，请稍后再试')
            res.send({status: 200, message: '添加成功，请等待管理员审核'})
        })
    })
    }else{
      db.query('select * from user_info where name=?', langResourceMes.owner, (err, result)=>{
        if(err) return res.cc('404', err.message)
        if(result.length< 1) return res.cc('404', '无此用户')
        var sql = 'insert into land_resource set ?'
        db.query(sql, {name:langResourceMes.name, parcelcount:langResourceMes.parcelcount, address:langResourceMes.address, userid:result[0].id}, (err, results)=>{
            if(err) return res.cc('404', err.message)
            if(results.affectedRows !== 1) return res.cc('404', '添加失败，请稍后再试')
            res.send({status: 200, message: '添加成功'})
        })
    })
    }

}

//更新
exports.up = (req, res)=>{
  if(req.body.tar == '1'){
    //用户
    db.query(`select * from land_resource where name=? and id=?`, [req.body.name,req.body.id], (err, result)=>{
      if(err) return res.cc('500', err)
      if(result.length == 1) {
        //没改名
        db.query('update land_resource  set parcelcount=?, address=? WHERE id=?', [req.body.parcelcount, req.body.address, req.body.id], (err, results)=>{
          if(err) return res.cc('404',err)
          if(results.affectedRows !== 1) return res.cc('500','修改土地资源信息失败')
          res.send({ //返回前端信息
              status: 200,
              message: '修改土地资源信息成功',
          })
      })
      }else{
        db.query(`select * from land_resource where name=?`, [req.body.name], (err, results2)=>{
          if(err) return res.cc('404',err)
          if(results2.length == 1){
            res.send({ //返回前端信息
              status: '400',
              message: '修改失败，存在同名',
          })
          }else{
            db.query('update land_resource  set name =? ,parcelcount=?, address=?, userid=? WHERE id=?', [req.body.name,req.body.parcelcount, req.body.address, req.body.id], (err, results3)=>{
              if(err) return res.cc('404',err)
              if(results3.affectedRows !== 1) return res.cc('500','修改土地资源信息失败')
              res.send({ //返回前端信息
                  status: 200,
                  message: '修改土地资源信息成功',
              })
          })
          }
        })
      }
  })
  }else{
    db.query(`select * from user_info where name=?`, req.body.owner, (err, result)=>{
      if(err) return res.cc('500', err)
      if(result.length < 1) return res.cc('404', '无此用户')
      db.query('update land_resource  set name=?, parcelcount=?, address=?, userid=? WHERE id=?', [req.body.name, req.body.parcelcount, req.body.address, result[0].id, req.body.id], (err, results)=>{
          if(err) return res.cc('404',err)
          if(results.affectedRows !== 1) return res.cc('500','修改土地资源信息失败')
          res.send({ //返回前端信息
              status: 200,
              message: '修改土地资源信息成功',
          })
      })
  })

  }
    
}
//删除
exports.delete = (req,res)=>{
    db.query('select * from land_parcel where landresourceid=?', req.query.id, (err, results) => {
        if(err) return res.cc('404', err.message)
        if(results.length< 1){
          db.query('DELETE FROM land_resource WHERE id = ?', req.query.id, (err, results1)=>{
            if(err) return res.cc('404', err.message)
            if(results1.affectedRows !== 1) return res.cc('500', '删除失败');
            res.send({status: 200, message: '删除成功'})
        })
        }
        else{
          res.send({status: '404', message: '删除失败，存在关联的土地块信息，请先删除'})
        }
      })
}